{extend name="base" /}
<!-- page specific plugin styles -->
{block name="inline-styles"}
<style>
.swiper-container {
	width: 100%;
} 

.swiper-container img {
	display: block;
	width: 100%;
	height:200px;
}
</style>
{/block}
<!-- PAGE CONTENT BEGINS -->
{block name="content"}
<div class="page js_show">
    <div class="page__bd" style="height: 100%">
        <div class="weui-tab">
            <div class="weui-tab__panel">
            	<div class="weui-panel">
            		<div class="weui-cells__title">收货地址</div>
					<div class="weui-cells">
						<a class="weui-cell weui-cell_access" href="{:url('Index/address_list')}">
							<div class="weui-cell__bd" id="address">
								<p>收货人：<span id="name"></span></p>
								<p>手机号：<span id="tel"></span></p>
								<p>地址：<span id="addrvalue"></span><span id="addr"></span></p>
							</div>
							<div class="weui-cell__ft"></div>
						</a>
					</div>
				</div>
				<div class="weui-panel">
					<div class="weui-panel__bd">
						<a href="javascript:void(0);" class="weui-media-box weui-media-box_appmsg"  id="formData">
		                    <div class="weui-media-box__hd">
		                        <img class="weui-media-box__thumb" src="" id='pic' alt="">
		                    </div>
		                    <div class="weui-media-box__bd">
		                        <h4 class="weui-media-box__title" id="goodsname"></h4>
		                        <p class="weui-media-box__desc" > <span id='price'></span>元</p>
		                    </div>
		                    <span class="weui-media-box__ft">x	<span id="number">1</span></span>
		                </a>
					</div>
				</div>
				<div class="weui-panel">
					<div class="weui-panel__hd">备注信息</div>
					<div class="weui-panel__bd">
			            <div class="weui-cell">
			                <div class="weui-cell__bd">
			                    <textarea class="weui-textarea" name="beizhu" id="beizhu" placeholder="请输入文本" rows="3"></textarea>
			                </div>
			            </div>
			        </div>
		        </div>

		        <div class="weui-panel">
		        	<div class="weui-cells__title">发货地址</div>
					<div class="weui-cells">
						<a class="weui-cell weui-cell_access" href="{:url('Index/address_start_list')}">
							<div class="weui-cell__bd" id="addressStart">
								<p>发货人：<span id="name"></span></p>
								<p>手机号：<span id="tel"></span></p>
								<p>地址：<span id="addrvalue"></span><span id="addr"></span></p>
							</div>
							<div class="weui-cell__ft"></div>
						</a>
					</div>
				</div>
            </div>
            <div class="weui-tabbar">

            	<div class="weui-tabbar__item"><span class="weui-wepay-color-red" style="font-size: 18px;" id="total_price">合计：1800</span></div>
                <div class="weui-tabbar__item" style="padding-top: 0">
                    <a class="weui-btn weui-btn_warn" href="javascript:;" id="showToast" style="border-radius: 0">立即支付</a>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}
<!-- PAGE CONTENT ENDS -->
<!-- inline scripts related to this page -->
{block name="related-scripts"}
<script type="text/javascript">

var hasSend = false;
	$("#number").text(GetQueryString("courier_num"));
	var _receive_addr_id=0;
	var _send_addr_id=0;
	if(localStorage.receive_add == undefined){
		// 默认收货地址
		$.ajax({
	        url: '/api/v1/address/defalt_addr',
	        type: 'POST',
	        dataType: 'json',
	        data:{
	            type: 1
	        }
	    })
	    .done(function(json) {
	        console.log("success");
	        if(json.data!=null){
		        _receive_addr_id=json.data.id;
		        initPageHTML("address",json.data);
		    }
	    })
	    .fail(function() {
	        console.log("error");
	    })
	    .always(function() {
	        console.log("complete");
	    });
    }else{
    	// 查询发货地址
    	console.log(localStorage.receive_add);
		$.ajax({
	        url: '/api/v1/address/find',
	        type: 'GET',
	        dataType: 'json',
	        data:{
	            id: localStorage.receive_add
	        }
	    })
	    .done(function(json) {
	        console.log("success");
	        if(json.data!=null){
		        _receive_addr_id= localStorage.receive_add;
		        initPageHTML("address",json.data);
		    }
	    })
	    .fail(function() {
	        console.log("error");
	    })
	    .always(function() {
	        console.log("complete");
	    });
    }


    if(localStorage.send_addr == undefined){
	    // 默认发货地址
	    $.ajax({
	        url: '/api/v1/address/defalt_addr',
	        type: 'POST',
	        dataType: 'json',
	        data:{
	            type: 2
	        }
	    })
	    .done(function(json) {
	        console.log("success");
	        if(json.data!=null){
	        	_send_addr_id=json.data.id;
	        	initPageHTML("addressStart",json.data);
	        }
	    })
	    .fail(function() {
	        console.log("error");
	    })
	    .always(function() {
	        console.log("complete");
	    });
    }else{
    	// 查询发货地址
    	console.log(localStorage.send_addr);
		$.ajax({
	        url: '/api/v1/address/find',
	        type: 'GET',
	        dataType: 'json',
	        data:{
	            id: localStorage.send_addr
	        }
	    })
	    .done(function(json) {
	        console.log("success");
	        if(json.data!=null){
		        _send_addr_id= localStorage.send_addr;
		        initPageHTML("addressStart",json.data);
		    }
	    })
	    .fail(function() {
	        console.log("error");
	    })
	    .always(function() {
	        console.log("complete");
	    });
    }


    // 商品信息
    $.ajax({
        url: '/api/v1/goods/homefind',
        type: 'POST',
        dataType: 'json',
        data:{
        	id: GetQueryString("goodsid")
        }
    })
    .done(function(json) {
        console.log("success");
        initPageHTML("formData",json.data);
        $('#pic').attr('src',json.data.pic)
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("complete");
    });
	// 计算价格
	var _price = GetQueryString("courier_num") * GetQueryString("price");
	$("#total_price").text("合计："+_price);
	
	$("#showToast").click(function(argument) {
		
		//防止重复提交
		if(hasSend ){
			return;
		}
		hasSend = true;
		// body...
		$.ajax({
	        url: '/api/v1/goodsorder/add',
	        type: 'POST',
	        dataType: 'json',
	        data:{
	        	goodsid: GetQueryString("goodsid"),
	        	courier_num: GetQueryString("courier_num"),
	        	key:GetQueryString("key"),
	        	receive_addr_id: _receive_addr_id,
	        	send_addr_id: _send_addr_id,
	        	beizhu: $("#beizhu").val()
	        }
	    })
	    .done(function(json) {
	        console.log("success");
	        if(json.returnCode==1){
                $.toast("支付成功", function() {
                    window.location.href = "{:url('Index/order_detail')}?id="+json.data.id;
                });
            }else{
                $.toptip(json.returnMsg, 'error');
            }
	    })
	    .fail(function() {
	        console.log("error");
	    })
	    .always(function() {
	        console.log("complete");
	    });
	});

	// 存储当前网址，用于选择地址返回
	localStorage.url = window.location.href;
</script>
{/block}